package com.self.study.subjectsstudy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.study.subjectsstudy.common.ResultInfo;
import com.self.study.subjectsstudy.db.bean.ErrorSubjects;
import com.self.study.subjectsstudy.db.mapper.ErrorSubjectsMapper;
import com.self.study.subjectsstudy.service.ISubjectsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lidd
 * @time 2021/3/8 14:20
 */
@RestController
@Slf4j
@RequestMapping("answer")
public class SubjectAnswerController {

    private static String AUTH_TOKEN = "Authorization";

    @Autowired
    private ISubjectsService subjectsService;

    @Autowired
    private ErrorSubjectsMapper errorSubjectsMapper;

    /**
     * 登录人答题操作
     *
     * @param httpServletRequest 请求
     * @param option             选项
     * @param subjectId          subject_id
     * @return 结果
     */
    @RequestMapping("subject_answer")
    public ResultInfo subjectAnswer(HttpServletRequest httpServletRequest,
                                    @RequestParam("option") String option,
                                    @RequestParam("subject_id") Integer subjectId) {
        String openid = httpServletRequest.getHeader(AUTH_TOKEN);
        return subjectsService.answerSubject(openid, option, subjectId);
    }

    /**
     * 获取我的错题
     *
     * @param httpServletRequest 请求
     * @return 结果集
     */
    @RequestMapping("my_error")
    public ResultInfo getMyErrorSubjects(HttpServletRequest httpServletRequest) {
        String openid = httpServletRequest.getHeader(AUTH_TOKEN);
        return subjectsService.getMyErrorSubjects(openid);
    }

    /**
     * 删除错题
     * @param httpServletRequest 请求
     * @param subjectId
     * @return
     */
    @RequestMapping("del_error")
    public ResultInfo deleteMyErrorSubjects(HttpServletRequest httpServletRequest,
                                            @RequestParam("subject_id") Integer subjectId) {
        String openid = httpServletRequest.getHeader(AUTH_TOKEN);
        errorSubjectsMapper.delete(new QueryWrapper<ErrorSubjects>().eq("openid", openid)
                .eq("subject_id", subjectId));
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("删除成功");
    }
}
